package com.bw.user.mvvm.viewmodel;

import android.animation.ValueAnimator;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.mvvm_comom.entity.BaseEntity;
import com.bw.mvvm_comom.mvvm.BaseViewModel;
import com.bw.user.mvvm.db.UserEntityValues;
import com.bw.user.mvvm.model.RegisterModel;
import com.bw.user.mvvm.model.entity.UserEventEntity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class RegisterViewModel extends BaseViewModel<RegisterModel> {
    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<String> verify = new ObservableField<>();
    public ObservableField<String> pwd = new ObservableField<>();
    public ObservableField<String> confirmpwd = new ObservableField<>();
    public ObservableField<String> timeStr = new ObservableField<>("获取验证码");

    @Override
    public void onCreate() {
        super.onCreate();

    }

    public void finish(){

        getUiChange().getFinishActObServAble().setValue(""+System.currentTimeMillis());
    }

    public void verify(){

    }


    public void confirm(){
        if (phone.get().isEmpty()){
            ToastUtils.showShort("请输入手机号");
            return;
        }
        if (pwd.get().isEmpty()){
            ToastUtils.showShort("请输入密码");
            return;
        }
        if (verify.get().isEmpty()){
            ToastUtils.showShort("请输入验证码");
            return;
        }
        if (confirmpwd.get().isEmpty()){
            ToastUtils.showShort("请再次输入密码");
            return;
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("mobile",phone.get());
        map.put("pwd",pwd.get());
        map.put("verifyCode",verify.get());
        m.requestRegister(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
        getUiChange().getFinishActObServAble().setValue(""+System.currentTimeMillis());
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity instanceof UserEventEntity){
            UserEventEntity userEventEntity = new UserEventEntity();
            userEventEntity.phone = phone.get();
            userEventEntity.pwd = pwd.get();
            userEventEntity.action = "update";
            EventBus.getDefault().post(userEventEntity);
            getUiChange().getFinishActObServAble().setValue(""+System.currentTimeMillis());
        }
    }
}
